from django.shortcuts import render, HttpResponse
import datetime
from app01 import models


# Create your views here.
def get_date_weekday():
    today = datetime.datetime.now()
    date = today.strftime('%Y年%m月%d日')
    week_name = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
    week_day = week_name[today.weekday()]
    return f'{date} {week_day}'


def spider(request):
    name_list = ['移动', '电信', '联通', '广电']
    name_str = ','.join(name_list)
    # return HttpResponse(name_str)
    return render(request, 'instructions.html',
                  {'name_list': name_list, 'weekday': get_date_weekday(), 'name_str': name_str})


def students(request):
    students_query = models.Students.objects.all()
    if request.method == 'GET':
        return render(request, 'students.html', {'weekday': get_date_weekday(),
                                                 'students_query': students_query})
    elif request.method == 'POST':
        name = request.POST.get('name')
        age = request.POST.get('age')
        nationality = request.POST.get('nationality')
        tel = request.POST.get('tel')
        email = request.POST.get('email')
        models.Students.objects.create(name=name, age=age, nationality=nationality, tel=tel, email=email)
        students_query = models.Students.objects.all()
        return render(request, 'students.html', {'weekday': get_date_weekday(),
                                                 'students_query': students_query})


def add_student(request):
    # add student
    models.Students.objects.create(name='金娜英', age=19, nationality='Korean', tel=155, email='neoyong@live.com')
    models.Students.objects.create(name='早田希娜', age=28, nationality='Japanese', tel=188, email='neoyong@live.com')
    models.Students.objects.create(name='张本美和', age=16, nationality='Japanese', tel=188, email='neoyong@live.com')
    models.Students.objects.create(name='田志希', age=22, nationality='Korean', tel=188, email='neoyong@live.com')
    models.Students.objects.create(name='孙颖莎', age=22, nationality='Chinese', tel=188, email='neoyong@live.com')
    # delete student
    # models.Students.objects.filter(id=7).delete()
    # update
    # models.Students.objects.filter(id=8).update(name='平原美野', age=38)
    # query
    # models.Students.objects.filter(id__gt=5)

    return HttpResponse('added successfully')
